package com.weibo.ssosdk;

import android.text.TextUtils;
import com.huawei.hms.common.data.DataBufferSafeParcelable;
import com.huawei.hwid.core.constants.HwAccountConstants;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReentrantLock;
import o.foe;
import o.fof;
import o.foi;
import org.json.JSONObject;

public class WeiboSsoSdk {
    private static WeiboSsoSdk ˊ;
    private static foi ˏ;
    private int ʽ;
    private b ˋ;
    private volatile ReentrantLock ˎ = new ReentrantLock(true);
    private boolean ॱ = true;

    public static final class b {
        private String ˋ = "";
        private String ˏ = "";

        public String ˋ() {
            return this.ˏ;
        }

        public String ˏ() {
            return this.ˋ;
        }

        static b ॱ(String str) throws Exception {
            b bVar = new b();
            try {
                JSONObject jSONObject = new JSONObject(str);
                String optString = jSONObject.optString("retcode", "");
                JSONObject jSONObject2 = jSONObject.getJSONObject(DataBufferSafeParcelable.DATA_FIELD);
                if (!optString.equals("20000000") || jSONObject2 == null) {
                    throw new Exception("error： " + optString + " msg:" + jSONObject.optString("msg", ""));
                }
                bVar.ˏ = jSONObject2.optString("aid", "");
                bVar.ˋ = jSONObject2.optString("sub", "");
                return bVar;
            } catch (Exception e) {
                throw e;
            }
        }
    }

    private native String riseWind(String str, String str2, String str3, String str4, String str5, String str6, String str7, String str8, String str9, String str10, int i, int i2);

    static {
        System.loadLibrary("wind");
    }

    private WeiboSsoSdk() throws Exception {
        if (ˏ == null || !ˏ.ॱ()) {
            throw new Exception("config error");
        }
        this.ʽ = 0;
        new Thread(new Runnable(this) {
            final /* synthetic */ WeiboSsoSdk ˎ;

            {
                this.ˎ = r1;
            }

            public void run() {
                while (true) {
                    try {
                        Thread.sleep(HwAccountConstants.CHECK_SITE_COUNTRY_DURATION);
                        String str = "";
                        if (this.ˎ.ˋ == null || TextUtils.isEmpty(this.ˎ.ˋ.ˋ())) {
                            str = this.ˎ.ˏ();
                        } else {
                            str = this.ˎ.ˋ.ˋ();
                        }
                        WeiboSsoSdk.ॱ().ˋ(str, 2);
                    } catch (Exception e) {
                    }
                }
            }
        }).start();
        new Thread(new Runnable(this) {
            final /* synthetic */ WeiboSsoSdk ˎ;

            {
                this.ˎ = r1;
            }

            public void run() {
                try {
                    Thread.sleep(60000);
                    if (this.ˎ.ॱ) {
                        String ˊ;
                        if (this.ˎ.ˋ == null || TextUtils.isEmpty(this.ˎ.ˋ.ˋ())) {
                            ˊ = this.ˎ.ˏ();
                        } else {
                            ˊ = this.ˎ.ˋ.ˋ();
                        }
                        this.ˎ.ˋ(ˊ, 2);
                    }
                } catch (Exception e) {
                }
            }
        }).start();
    }

    public static synchronized boolean ˎ(foi o_foi) {
        boolean z = false;
        synchronized (WeiboSsoSdk.class) {
            if (o_foi != null) {
                if (o_foi.ॱ() && ˏ == null) {
                    ˏ = (foi) o_foi.clone();
                    foe.ˋ(ˏ.ˋ());
                    z = true;
                }
            }
        }
        return z;
    }

    public static synchronized WeiboSsoSdk ॱ() throws Exception {
        WeiboSsoSdk weiboSsoSdk;
        synchronized (WeiboSsoSdk.class) {
            if (ˊ == null) {
                ˊ = new WeiboSsoSdk();
            }
            weiboSsoSdk = ˊ;
        }
        return weiboSsoSdk;
    }

    private String ˎ(String str) {
        try {
            HttpURLConnection httpURLConnection = (HttpURLConnection) new URL("https://login.sina.com.cn/visitor/signin").openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setReadTimeout(3000);
            httpURLConnection.setConnectTimeout(1000);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setUseCaches(false);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            outputStream.write(str.getBytes());
            outputStream.flush();
            if (httpURLConnection.getResponseCode() != 200) {
                return null;
            }
            InputStream inputStream = httpURLConnection.getInputStream();
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] bArr = new byte[WXMediaMessage.DESCRIPTION_LENGTH_LIMIT];
            while (true) {
                int read = inputStream.read(bArr);
                if (read != -1) {
                    byteArrayOutputStream.write(bArr, 0, read);
                } else {
                    inputStream.close();
                    byteArrayOutputStream.close();
                    return new String(byteArrayOutputStream.toByteArray());
                }
            }
        } catch (Exception e) {
            return null;
        }
    }

    private void ˋ(String str, int i) throws Exception {
        if (!TextUtils.isEmpty(ˏ.ʼ(false))) {
            if (this.ˎ.tryLock()) {
                String encode;
                this.ॱ = false;
                String ॱ = foe.ॱ(ˏ.ˋ());
                try {
                    encode = URLEncoder.encode(str, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    encode = "";
                }
                String ˎ = ˎ(riseWind(ˏ.ʼ(true), ˏ.ˋ().getPackageName(), encode, ॱ, ˏ.ˋ(true), ˏ.ˊ(true), ˏ.ˎ(true), ˏ.ॱ(true), ˏ.ʻ(true), ˏ.ˏ(true), i, this.ʽ));
                this.ʽ++;
                if (ˎ != null) {
                    try {
                        b ॱ2 = b.ॱ(ˎ);
                        if (!(ॱ2 == null || TextUtils.isEmpty(ॱ2.ˋ()))) {
                            ˋ(ॱ2.ˋ());
                        }
                        if (i == 1) {
                            this.ˋ = ॱ2;
                        }
                        this.ˎ.unlock();
                        return;
                    } catch (Exception e2) {
                        this.ˎ.unlock();
                        throw e2;
                    }
                }
                this.ˎ.unlock();
                throw new Exception("network error.");
            }
            this.ˎ.lock();
            this.ˎ.unlock();
        }
    }

    public void ˏ(final fof o_fof) {
        if (this.ˋ == null || TextUtils.isEmpty(this.ˋ.ˋ()) || TextUtils.isEmpty(this.ˋ.ˏ())) {
            Executors.newSingleThreadExecutor().execute(new Runnable(this) {
                final /* synthetic */ WeiboSsoSdk ˊ;

                public void run() {
                    try {
                        this.ˊ.ˋ("", 1);
                    } catch (Exception e) {
                    }
                    if (this.ˊ.ˋ == null) {
                        this.ˊ.ˋ = new b();
                    }
                    o_fof.ˏ(this.ˊ.ˋ);
                }
            });
        } else {
            o_fof.ˏ(this.ˋ);
        }
    }

    private String ˏ() {
        Throwable th;
        FileInputStream fileInputStream = null;
        FileInputStream fileInputStream2;
        try {
            fileInputStream2 = new FileInputStream(ˎ(1));
            try {
                byte[] bArr = new byte[fileInputStream2.available()];
                fileInputStream2.read(bArr);
                String str = new String(bArr);
                if (fileInputStream2 == null) {
                    return str;
                }
                try {
                    fileInputStream2.close();
                    return str;
                } catch (IOException e) {
                    return str;
                }
            } catch (Exception e2) {
                fileInputStream = fileInputStream2;
                if (fileInputStream != null) {
                    try {
                        fileInputStream.close();
                    } catch (IOException e3) {
                    }
                }
                return "";
            } catch (Throwable th2) {
                th = th2;
                if (fileInputStream2 != null) {
                    try {
                        fileInputStream2.close();
                    } catch (IOException e4) {
                    }
                }
                throw th;
            }
        } catch (Exception e5) {
            if (fileInputStream != null) {
                fileInputStream.close();
            }
            return "";
        } catch (Throwable th3) {
            Throwable th4 = th3;
            fileInputStream2 = null;
            th = th4;
            if (fileInputStream2 != null) {
                fileInputStream2.close();
            }
            throw th;
        }
    }

    private File ˎ(int i) {
        return new File(ˏ.ˋ().getFilesDir(), "weibo_sso_sdk_aid" + i);
    }

    private synchronized void ˋ(String str) {
        FileOutputStream fileOutputStream;
        Throwable th;
        if (!TextUtils.isEmpty(str)) {
            FileOutputStream fileOutputStream2 = null;
            try {
                fileOutputStream = new FileOutputStream(ˎ(1));
                try {
                    fileOutputStream.write(str.getBytes());
                    if (fileOutputStream != null) {
                        try {
                            fileOutputStream.close();
                        } catch (IOException e) {
                        }
                    }
                } catch (Exception e2) {
                    if (fileOutputStream != null) {
                        try {
                            fileOutputStream.close();
                        } catch (IOException e3) {
                        }
                    }
                    return;
                } catch (Throwable th2) {
                    Throwable th3 = th2;
                    fileOutputStream2 = fileOutputStream;
                    th = th3;
                    if (fileOutputStream2 != null) {
                        try {
                            fileOutputStream2.close();
                        } catch (IOException e4) {
                        }
                    }
                    throw th;
                }
            } catch (Exception e5) {
                fileOutputStream = null;
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
                return;
            } catch (Throwable th4) {
                th = th4;
                if (fileOutputStream2 != null) {
                    fileOutputStream2.close();
                }
                throw th;
            }
        }
        return;
    }
}
